
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;

import com.sun.opengl.util.*; 

public class JOGLSphericalTest implements GLEventListener, KeyListener, MouseMotionListener, MouseListener {

	protected float pyramidRotation;
	protected float cubeRotation;
	int x,y,startX,startY;
	int mouseDown = 0;
	
	final double PI = 3.14159265358979323846;
	final double PI_DIV_180 = 0.017453292519943296;
	final double deg = PI_DIV_180;
	double trot = 30;
	double prot = 30;
	double r = 1.0;
	float strafeX = 0.0f;
	float strafeY = 0.0f;
	float strafeZ = 0.0f;
	
	public void display(GLAutoDrawable drawable) {
		final GL gl = drawable.getGL();
		final GLU glu = new GLU();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		

		
		gl.glLoadIdentity();
		gl.glTranslatef(0.0f, 0.0f, -1.0f);
	    //drawSpheres(gl);
		draw(gl,glu);			
						
	}
	
	protected void drawSpheres(GL gl, GLU glu){
		GLUT glut = new GLUT();
		
		
	    GLUquadric test; 
	    test = glu.gluNewQuadric();
	    gl.glColor3f(1.0f, 0.0f, 0.0f);
	    glu.gluQuadricDrawStyle(test,GLU.GLU_FILL);
	    glu.gluQuadricNormals(test,GLU.GLU_SMOOTH);
	    glu.gluSphere(test, 0.2, 100, 100);
	    
	    gl.glColor3f(0.0f, 0.0f, 0.0f);
	    glut.glutWireSphere(0.21, 25, 25);
	    
	
	}
	protected void draw(GL gl,GLU glu)
	{
		glu.gluLookAt(r*Math.cos(prot*deg)*Math.cos(trot*deg),r*Math.sin(prot*deg),r*Math.cos(prot*deg)*Math.sin(trot*deg), 0,0,0, 0,1,0);
		//gl.glTranslatef(strafeX,strafeY,strafeZ);
		drawSpheres(gl,glu);
		
	}
	
		public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {

	}

	public void init(GLAutoDrawable drawable) {
		final GL gl = drawable.getGL();
		final GLCapabilities caps = new GLCapabilities();
		caps.setDoubleBuffered(true);
		gl.glShadeModel(GL.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepth(1.0f);
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthFunc(GL.GL_LEQUAL);
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		drawable.addKeyListener(this);
		drawable.addMouseListener(this);
		drawable.addMouseMotionListener(this);
		
	}
	
	 public void keyPressed(KeyEvent e) {
			if(e.getKeyCode() == KeyEvent.VK_W) {
				r-=0.1;
				if(r <= 0.1) 
				{r= 0.1;}
			
			}
			
			if(e.getKeyCode() == KeyEvent.VK_S) {
			    r+=0.1;
			}
			
			if(e.getKeyCode() == KeyEvent.VK_A) {
			    strafeX+=0.1;
			}
			
		    }
		 
		    public void keyReleased(KeyEvent e) {
		    }
		 
		    public void keyTyped(KeyEvent e) {
		    }


		    
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				
				startX = e.getX();
			    startY = e.getY();
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseDragged(MouseEvent e) {
		    	
				int x = e.getX();
			    int y = e.getY();
				
				trot += (x-startX)/2;
		        if (trot > 360) trot -= 360;
		        if (trot < 0) trot += 360;
		        startX = x;

		        prot += (y-startY)/2;
		        if (prot < -90) prot = -90;
		        if (prot > 90) prot = 90;
		        startY = y;
		        
		        			
			}

			@Override
			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		    
		    
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		final GL gl = drawable.getGL();
		final GLU glu = new GLU();
		
		gl.setSwapInterval(0);

		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();

		glu.gluPerspective(
				45.0f, 
				(double) width / (double) height, 
				0.1f,
				1000.0f);

		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	

	



}